package com.example.locationreport.locationreport.utils;

import android.util.Xml;

import com.example.locationreport.locationreport.bean.CildXmlBean;

import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;

/**
 * author : King
 * date   : 2022/12/221:23
 * desc   :
 */
public class XmlUtils {
    private static final String TAG = XmlUtils.class.getSimpleName();

    public static CildXmlBean readXml(String path){

        CildXmlBean cildXmlBean = new CildXmlBean();

        try {
            File file = new File(path);

            InputStream inputStream = new FileInputStream(file);
            XmlPullParser parser = Xml.newPullParser();
            parser.setInput(inputStream, "UTF-8");
            try {
                while (parser.getEventType() != XmlPullParser.END_DOCUMENT) {

                    if (parser.getEventType() == XmlPullParser.START_TAG) {
                        if(parser.getDepth() == 3) {
                            if ("date".equals(parser.getName())){
                                cildXmlBean.setSysVersionName(CommonUtils.getNormalString(parser.nextText()));
                            }

                            if ("version".equals(parser.getName())){
                                cildXmlBean.setSysVersion(CommonUtils.getNormalString(parser.nextText()));
                            }
                        }
                    }
                    parser.next();
                }
                inputStream.close();
            }catch (XmlPullParserException e) {
                e.printStackTrace();
            }
        }  catch (IOException | XmlPullParserException e) {
            LogLocationUtils.writeLog(TAG,"获取系统版本异常--"+e.getMessage());
        }
        return cildXmlBean;
    }
}
